package com.tianqu.tms.www.car.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.car.actionform.CarAutoTerminalManageActionForm;
import com.tianqu.tms.www.car.viewhelper.CarAutoTerminalManageViewHelper;

/**
 * 自助打印 - 新车上牌一览 controller
 * @author 
 */
public class CarAutoTerminalSecondHandDetailController extends TmsController {
	private Logger logger = Logger
			.getLogger(CarAutoTerminalBrandNewCarDetailController.class);
	public static final String PRIVILEGEDCARLIST = "privilegedCarList";

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		CarAutoTerminalManageViewHelper view = new CarAutoTerminalManageViewHelper();
		// 添加事务提交异常捕获
		try {
			controller.begin();
			String lcPrintBsFlag = uc.getParameter("lcPrintBsFlag");
			CarAutoTerminalManageActionForm form = (CarAutoTerminalManageActionForm) uc.getInputForm();
			Car car = Car.getInstance();
			// 查询
			TransferAcceptedInfoDto dto = new TransferAcceptedInfoDto();
			String lPlate = Registry.getConfig(TmsConstants.TMS_DEFLICENSEPLATE_KEY);
			String licensePlateLast = form.getLicensePlateLast();
			if(StringUtil.isNotBlank(lcPrintBsFlag)) licensePlateLast = new String(form.getLicensePlateLast().getBytes("ISO8859-1"), "UTF-8");
			dto.setLicensePlate(lPlate + licensePlateLast);
			dto.setLicensePlateCol(form.getLicensePlateCol());
			List<TransferAcceptedInfoDto> list = car.loadCarAutoTerminalSecondHandList(dto);
			view.setViewList(list);
			
			// 打印内容获取及更新
			
			if (!StringUtil.isNullOrBlank(lcPrintBsFlag) 
					&& lcPrintBsFlag.equals("1")) {
				car.updateTransferCyPrintNumByLicensePlate(list.get(0));
//				dto =  (TransferAcceptedInfoDto)list.get(0);
//				String serial = dto.getSerial();
//				// 打印内容
//				TransferAcceptedInfo entity = TransferAcceptedInfo.getInstance();
//				entity = entity.findPirntNumBySerial(serial);
////				TmsUserProfile profile = uc.getUserProfile();
//				TransferAcceptedInfo transferAcceptedInfo = new TransferAcceptedInfo();
//				transferAcceptedInfo.setSerial(serial);
//				transferAcceptedInfo.setCy_printNum(entity.getCy_printNum()+1);
//				transferAcceptedInfo.setLc_printNum(entity.getLc_printNum());
//				transferAcceptedInfo.setUpdateUserId(1);
//				transferAcceptedInfo.setCarOwner(entity.getCarOwner());
//				transferAcceptedInfo.updateTransferCyPrintNumBySerial(transferAcceptedInfo);
//				
//				view.setTransferAcceptedInfo(entity);
			}
			
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
		}
}
